package ui.server;
import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.net.SocketException;
import java.util.ArrayList;

import util.IEventCallback;
import net.ClientThread;


public class FileTransferServer implements IEventCallback{
	public static final int NUM_THREADS = 4;
	private int port;
	private String targetDir;
	private JFileTransferServerApp frame = null;
	private ServerSocket serversocket = null;
	private volatile boolean stopRequested = false;
	public ArrayList clientThreadPool;
	
	public FileTransferServer(int port, String targetDir) { 
	    this.port = port; 
	    this.targetDir = targetDir;
	} 
	
	public void setCallback(JFileTransferServerApp f)
	{
		if(frame == null)
			frame = f;
	}
	
	public void startServer() throws IOException
	{
		try {
			if( serversocket == null)
			{
				stopRequested = false;
				serversocket = new ServerSocket(Integer.valueOf(this.port));
				this.callback(IEventCallback.EVT_SERVER_CONN_START);
				clientThreadPool = new ArrayList();
	    		while (!stopRequested) {	    			
	    				Socket s = serversocket.accept();
	    				ClientThread clientThread = new ClientThread(s, this);
	    				clientThreadPool.add(clientThread);
	    			    Thread client = new Thread(clientThread);
	    			    client.start();
	            }    		
			}					
        }  catch (SocketException e) {
    		//this.callback(e.getMessage(), IEventCallback.ERR_SOCKET_EXCEPTION);
        	System.out.println("[startServer] SocketException: " + e.getMessage().toString());
        } catch (IOException e) { 
        	this.callback(e.getMessage(), IEventCallback.ERR_IO_EXCEPTION);
			System.out.println("[startServer] IOException: " + e.getMessage().toString());
        } 
	}
	
	public void stopServer() throws Exception	{
		System.out.println("[stopServer]");
		stopRequested = true;
		try {
			if( serversocket != null) {
				for(int i = 0; i < clientThreadPool.size(); i++)
				{
					ClientThread t = (ClientThread)clientThreadPool.get(i);
					t.stopRequest();
				}
				clientThreadPool.clear();
				serversocket.close();
				serversocket = null;
				this.callback(IEventCallback.EVT_SERVER_CONN_CLOSE);
			}
		} catch (IOException e) {
			e.printStackTrace();
			this.callback(e.getMessage(), IEventCallback.ERR_IO_EXCEPTION);
		}
	}
	
	public String getTargetDir() {
		return targetDir;
	}

	public void callback(long... argv) {
		frame.doEvtCallback(IEventCallback.CALLBACK_TYPE_SERVER, argv);
	}
	
	public void callback(String message, long... argv) {
		frame.doErrCallback(IEventCallback.CALLBACK_TYPE_SERVER, message, argv);
	}
	
	
		
//    public static void main(String port) { 
//    	try {
//    		ServerSocket serversock = new ServerSocket(Integer.valueOf(port)); 
//            while (true) {
//            	Socket s = serversock.accept(); 
//                Thread client = new Thread(new ClientThread(s)); 
//                client.start(); 
//            }  
//        } catch (IOException e) { 
//        	e.printStackTrace(); 
//        } 
//    } 
}
